﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using SuppKids.Core.Domain;
using SuppKids.Core.Extensions;
using SuppKids.ViewModels.MenuRaskladka;

namespace SuppKids.Converters
{
	public class ResultListItemConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			var source = value as ResultListItem;
			if (source != null)
			{
				var data = parameter as object[];
				if (data != null)
				{

					if (data[0] is int && data[1] is DailyMenu[])
					{
						var groupId = (int) data[0];
						var dailyMenus = (DailyMenu[]) data[1];
						return
							source.ConsumptionValues
							.FirstOrDefault(a => a.Group.Id == groupId && a.DailyMenu == dailyMenus.FirstOrDefault(b => b.MealType == source.MealType)).With(a => a.Value);
					}
				}
			}
			return 0;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}